package com.cryptoneedle.garden.service.properties;


import com.cryptoneedle.garden.enums.SourceDatabaseType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * <p>description: 目标配置项 </p>
 * <p>
 * 需在Doris的所有FE、BE上安装数据库的驱动包
 *
 * @author CryptoNeedle
 * @date 2024-09-20
 */
@Data
@Component
@ConfigurationProperties(prefix = "doris")
@Schema(description = "Doris配置项")
public class DorisProperties {

    @Schema(description = "主机")
    private String host;

    @Schema(description = "端口")
    private Integer port;

    @Schema(description = "用户名")
    private String username;

    @Schema(description = "密码")
    private String password;

    private Catalog catalog = new Catalog();

    @Data
    @Schema(description = "目录配置项")
    public static class Catalog {

        private ConnectionPool connectionPool = new ConnectionPool();

        @Data
        @Schema(description = "连接池")
        public static class ConnectionPool {

            private Integer minSize = 1;
            private Integer maxSize = 30;
            private Integer maxWaitTime = 10000;
            private Integer maxLifeTime = 1800000;
            private Boolean keepAlive = true;
        }

        private Database oracle = new Oracle();
        private Database mysql = new MySql();
        private Database pg = new PostgreSql();
        private Database sqlserver = new SqlServer();

        @Data
        @Schema(description = "Oracle配置项")
        public abstract class Database {

            @Schema(description = "驱动Jar包名称")
            private String driverJar;

            @Schema(description = "驱动类路径")
            private String driverClass;
        }

        @Schema(description = "Oracle 配置项")
        public class Oracle extends Database {
        }

        @Schema(description = "MySql 配置项")
        public class MySql extends Database {
        }

        @Schema(description = "PostgreSql 配置项")
        public class PostgreSql extends Database {
        }

        @Schema(description = "SqlServer 配置项")
        public class SqlServer extends Database {
        }
    }

    public String getDriverJar(SourceDatabaseType databaseType) {
        return switch (databaseType) {
            case ORACLE -> this.catalog.oracle.driverJar;
            case MYSQL -> this.catalog.mysql.driverJar;
            case POSTGRESQL -> this.catalog.pg.driverJar;
            case SQLSERVER -> this.catalog.sqlserver.driverJar;
        };
    }

    public String getDriverClass(SourceDatabaseType databaseType) {
        return switch (databaseType) {
            case ORACLE -> this.catalog.oracle.driverClass;
            case MYSQL -> this.catalog.mysql.driverClass;
            case POSTGRESQL -> this.catalog.pg.driverClass;
            case SQLSERVER -> this.catalog.sqlserver.driverClass;
        };
    }
}